
import { setTokenStorage } from './token';
import logger from './logger';
import { genTestUserSig } from '../debug/GenerateTestUserSig.js';
const { getTokenStorage } = require('./token.js');
import Vue from 'vue'
var chooseImg = function(count){
	return new Promise(function(resolve,reject){
		let imgs = []
		uni.chooseImage({
			count: count,
			success(res) {
				res.tempFilePaths.forEach(item=>{
					imgs.push(item)
				})
				resolve(imgs)
			}
		})
	})
}
var loginIM = function(uid) {
	const app = getApp()

	return new Promise(function(resolve,reject){
		var that = this
		const userID = uid + ''
		const userSig = genTestUserSig(userID).userSig;
		const SDKAppID = app.globalData.SDKAppID;
		
		
		
		
		// #ifdef  APP-PLUS
		uni.$aegis.reportEvent({
		    name: 'platform',
		    ext1: 'platform-APP',
		    ext2: 'uniTuikitExternal',
		    ext3: `${SDKAppID}`,
		})
		// #endif
		app.globalData.userInfo = {
			userSig,
			userID
		};
		setTokenStorage({
			userInfo: app.globalData.userInfo
		});
		uni.setStorageSync(`TIM_${getApp().SDKAppID}_isTUIKit`, true);

		uni.$TUIKit.login({
			userID: userID,
			userSig: userSig
		}).then((val) => {
			uni.$aegis.reportEvent({
			    name: 'login',
			    ext1: 'login-success',
			    ext2: 'uniTuikitExternal',
			    ext3: `${SDKAppID}`,
			})
			resolve()
		}).catch((error) => {
			console.log(error)
			uni.$aegis.reportEvent({
			    name: 'login',
			    ext1: `login-failed#error:${error}`,
			    ext2: 'uniTuikitExternal',
			    ext3: `${SDKAppID}`,
			})
		})
		
		// 登录原生插件
		// #ifdef APP-PLUS
		if(typeof(uni.$TUICallKit) == 'undefined') {
			uni.showToast({
				title: '如果需要音视频功能，请集成插件使用真机运行并且自定义基座调试哦～',
				icon: 'none',
				duration: 3000
			});
		} else {
			// uni.$TUICalling.login(
			// 	{
			// 		sdkAppID: SDKAppID,
			// 		userID: userID,
			// 		userSig: userSig
			// 	},
			// 	res => {
			// 		console.log(JSON.stringify(res.msg));
			// 		uni.showToast({
			// 			title: 'login',
			// 			icon: 'none'
			// 		});
			// 	}
			// );
			const talkoptions = {
				SDKAppID: SDKAppID,
				userID: userID,
				userSig: userSig
			}
			console.log(talkoptions)
			uni.$TUICallKit.login(talkoptions, (res) => {
			    if (res.code === 0) {
			        console.log('login success');
			    } else {
			        console.log(res);
			    }
			});

		}
		// #endif
	})
}

// pics 图片数组
var uploadFiles = function(pics){
	var that = this
	console.log(this)
	let imgs = []
	let upurl = Vue.prototype.$upurl
  return new Promise(function(resolve,reject){
		pics.forEach(item=>{
			uni.showLoading({
				title: "上传中"
			})
			uni.uploadFile({
				url: upurl,
				filePath: item,
				name: "file",
				success(res) {
					let data = JSON.parse(res.data)
					console.log(data)
					imgs.push({
						name: data.data.savename,
						url: data.data.url
					})
					resolve(imgs)
				},
				fail(err){
					console.log(err)
				},
				complete(){
					uni.hideLoading()
				}
			})
		})
	})
}

export default {
	chooseImg,
	loginIM,
	uploadFiles
}